package action;

import config.ApplicationConfig;
import server.LoginServer;
import server.UserServer;
import util.Decode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-14 Sunday 16:50:42
 */
public class ChangeUserInformationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                String oldUsername = request.getParameter("oldUsername");
                String changeUser = request.getParameter("changeUser");
                String realName = request.getParameter("realName");
                String sex = request.getParameter("sex");
                String clothesSize = request.getParameter("clothes_size");
                String phone = request.getParameter("phone");
                String email = request.getParameter("email");
                if (oldUsername != null && changeUser != null && realName != null && clothesSize != null && sex != null && phone != null && email != null) {
                    Decode decode = new Decode();
                    changeUser = decode.urlUtf8Decode(changeUser);
                    realName = decode.urlUtf8Decode(realName);
                    sex = decode.urlUtf8Decode(sex);
                    sex = sex.equals("男") ? sex : "女";
                    clothesSize = decode.urlUtf8Decode(clothesSize).toUpperCase();
                    clothesSize = (clothesSize.equals("S") || clothesSize.equals("M") || clothesSize.equals("L") || clothesSize.equals("XL") || clothesSize.equals("XXL") || clothesSize.equals("XXXL")) ? clothesSize : "L";
                    phone = decode.urlUtf8Decode(phone);
                    email = decode.urlUtf8Decode(email);
                    UserServer userServer = new UserServer();
                    userServer.open();

                    int canChange = userServer.canChange(username, oldUsername);

                    switch (canChange) {
                        case 0:
                            session.setAttribute("infoTitle", "修改信息");
                            session.setAttribute("infoContent", "修改失败，你没有权限修改");
                            session.setAttribute("return", "mine");
                            session.setAttribute("panelColor", "red");
                            break;
                        default:
                            if (oldUsername.equals(changeUser) || userServer.searchUserByName(changeUser).size() == 0) {
                                userServer.changeUserInformation(oldUsername, changeUser, realName, sex, clothesSize, phone, email);
                                session.setAttribute("user", changeUser);
                                session.setAttribute("infoTitle", "修改信息");
                                session.setAttribute("infoContent", "修改成功");
                                session.setAttribute("return", "mine");
                                session.setAttribute("panelColor", "blue");
                            } else {
                                session.setAttribute("infoTitle", "修改信息");
                                session.setAttribute("infoContent", "修改失败，用户名已被占用");
                                session.setAttribute("return", "mine");
                                session.setAttribute("panelColor", "red");
                            }
                            break;
                    }

                    userServer.close();
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
